/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components.radio;

import com.bff.bjj.ui.component.MusicList;
import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistAddRadioAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistInsertRadioAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistLoadRadioAction;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.SlimRadioPlugin;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.musicobjects.radio.SlimRadioAudioDetails;

/**
 *
 * @author bfindeisen
 */
public class RadioItemList extends MusicList implements MouseListener {

    private SlimRadioPlugin radioPlugin;
    private DefaultListModel podcastListModel;
    private SlimPlaylist playlist;
    private RadioItemListCellRenderer radioListCellRenderer;
    private List<SlimRadioAudioDetails> podcastItemList;
    private JPopupMenu podcastMenu;

    public RadioItemList() {
        try {
            playlist = JukeboxProperties.getInstance().getPlaylist();
        } catch (PlaylistException ex) {
            Logger.getLogger(RadioItemList.class.getName()).log(Level.SEVERE, null, ex);
        }
        podcastListModel = new DefaultListModel();
        try { 
            setRadioPlugin(JukeboxProperties.getInstance().getRadioPlugin());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(RadioItemList.class.getName()).log(Level.SEVERE, null, ex);
        }

        setRadioListCellRenderer(new RadioItemListCellRenderer());
        setCellRenderer(getRadioListCellRenderer());

        //listAll();
        addMouseListener(this);

    }

    public void setRadioItems(Collection<SlimRadioAudioDetails> podcastItems) {
        podcastItemList = new ArrayList<SlimRadioAudioDetails>(podcastItems);

        podcastListModel.clear();

        for (SlimRadioAudioDetails item : podcastItemList) {
                podcastListModel.addElement(item);
        }

        setModel(podcastListModel);
        setRadioListCellRenderer(new RadioItemListCellRenderer());
        setCellRenderer(getRadioListCellRenderer());
        setSelectedIndex(0);
    }

    public SlimRadioAudioDetails getSelectedRadio() {
        return (SlimRadioAudioDetails) getSelectedValue();
    }

    public Collection<SlimRadioAudioDetails> getSelectedRadios() {
        List<SlimRadioAudioDetails> podcastItems = new ArrayList<SlimRadioAudioDetails>();
        for (Object o : getSelectedValues()) {
            podcastItems.add((SlimRadioAudioDetails) o);
        }

        return podcastItems;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    /**
     * @return the podcaster
     */
    public SlimRadioPlugin getRadioPlugin() {
        return radioPlugin;
    }

    /**
     * @param podcaster the podcaster to set
     */
    public void setRadioPlugin(SlimRadioPlugin podcaster) {
        this.radioPlugin = podcaster;
    }

    /**
     * @return the podcastListCellRenderer
     */
    public RadioItemListCellRenderer getRadioListCellRenderer() {
        return radioListCellRenderer;
    }

    /**
     * @param podcastListCellRenderer the podcastListCellRenderer to set
     */
    public void setRadioListCellRenderer(RadioItemListCellRenderer radioListCellRenderer) {
        this.radioListCellRenderer = radioListCellRenderer;
    }

    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            SlimRadioAudioDetails item = getSelectedRadio();

            //@TODO Podcast details
            //new AlbumThumbnailDialog(Utils.getParentComponent(this), song).setVisible(true);

        } else {
            createPodcastPlayPopUp(e);
        }
    }

    public void mousePressed(MouseEvent e) {
        createPodcastPlayPopUp(e);
    }

    public void mouseReleased(MouseEvent e) {
        createPodcastPlayPopUp(e);
    }

    public void mouseEntered(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    private void createPodcastPlayPopUp(MouseEvent e) {
        if (e.isPopupTrigger()) {
            setSelectedIndex(locationToIndex(e.getPoint()));
            SlimRadioAudioDetails radio = getSelectedRadio();
            podcastMenu = new JPopupMenu();
            podcastMenu.add(new JMenuItem(new PlaylistAddRadioAction(getPlaylist(), radio)));
            podcastMenu.add(new JMenuItem(new PlaylistInsertRadioAction(getPlaylist(), radio)));
            podcastMenu.add(new JMenuItem(new PlaylistLoadRadioAction(getPlaylist(), radio)));
            //podcastMenu.add(new JMenuItem(new ViewRadioAction(Utils.getParentComponent(this), radio)));

            podcastMenu.setVisible(true);
            podcastMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }
}
